<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller;

use App\Common\ValidationHelper;
use App\Exception\BaseException;
use App\Repository\CatalogRepository;
use App\Repository\MediaRepository;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Psr\Http\Message\ResponseInterface;
use Hyperf\Di\Annotation\Inject;

/**
 * Class IndexController
 * @package App\Controller
 * @Controller
 *
 */
class IndexController extends AbstractController
{
    /**
     * @Inject
     * @var CatalogRepository
     */
    protected CatalogRepository $catalogRepository;


    /**
     * @Inject
     * @var MediaRepository
     */
    protected MediaRepository $mediaRepository;

    /**
     * Notes: 获取列表
     * Author: Bruce.z
     * @RequestMapping(path="list",method="get")
     * DateTime: 2022/1/5 11:00
     * @return ResponseInterface
     * @throws BaseException
     */
    public function getList(): ResponseInterface
    {
        $param = ValidationHelper::getInstance()->make($this->request->all(),
            ['page' => 'min:1|required', 'size' => 'min:1|required'],
            ['page.*' => '页码参数错误', 'size.*' => '页码参数错误']
        );

        $res = $this->mediaRepository->index($param);
        return $this->success($res);
    }

    /**
     * Notes: 添加目录
     * Author: Bruce.z
     * @RequestMapping(path="addFolder",method="post")
     * @return ResponseInterface
     * @throws BaseException
     */
    public function addFolder(): ResponseInterface
    {
        $param = ValidationHelper::getInstance()->make($this->request->all(),
            ['pid' => 'min:0|required|integer','id' => 'min:0|required|integer', 'name' => 'required|string'],
            ['pid.*' => 'pid参数错误', 'name.*' => 'name参数错误']
        );

        $id = $param['id'];unset($param['id']);
        if($id == 0){
            $res = $this->catalogRepository->baseAdd($param);
        }else{
            $this->catalogRepository->updateByConditions(['id'=>$id ], $param);
            $res = $this->catalogRepository->getOne(['id'=>$id ]);
        }

        return $this->success($res);
    }

    /**
     * Notes: 获取分类树
     * @RequestMapping(path="folderList",method="get")
     * Author: Bruce.z
     * DateTime: 2022/8/8 17:39
     * @return ResponseInterface
     */
    public function folderList(): ResponseInterface
    {
        return $this->success($this->catalogRepository->makeSelectTree());
    }

    /**
     * Notes:
     * @RequestMapping(path="moveFolder",method="post")
     * Author: Bruce.z
     * DateTime: 2022/8/8 18:04
     * @return ResponseInterface
     * @throws BaseException
     */
    public function moveFolder(): ResponseInterface
    {
        $param = ValidationHelper::getInstance()->make($this->request->all(),
            ['image_ids' => 'array|required','image_ids.*' => 'integer|min:1', 'pid' => 'required|integer'],
            ['pid.*' => 'pid参数错误', 'image_ids.*' => 'image_ids参数错误']
        );

        $res = $this->mediaRepository->updateByConditions(['id'=>$param['image_ids']], ['catalog_id'=>$param['pid']]);
        return $this->success($res);
    }

}
